(function () {
    let h = window.innerHeight;
    let w = window.innerWidth;

    let bg = document.getElementsByClassName("box_bg");

    if ((w / h) >= (1920 / 1080)) {
        for (let i = 0; i < bg.length; i++) {
            bg[i].style.width = w + 'px';
            bg[i].style.height = w * (1080 / 1920) + 'px';
            bg[i].style.top = -(w * (1080 / 1920) - h) / 2 + 'px';
            bg[i].style.left = '0';
        }
    } else {
        for (let i = 0; i < bg.length; i++) {
            bg[i].style.height = h + 'px';
            bg[i].style.width = h * (1920 / 1080) + 'px';
            bg[i].style.left = -(h * (1920 / 1080) - w) / 2 + 'px';
            bg[i].style.top = '0';
        }
    }

    let box01_index = 0;
    let box01_p = document.getElementById('box01_text').children;

    function boxOne() {
        if (box01_index >= 0 && box01_index <= 2) {
            box01_p[box01_index].style.opacity = '1';
            box01_index++;
        } else {
            clearInterval(boxOneTimer);
        }
    }

    let x_arr = new Array();
    let y_arr = new Array();
    let x = y = m = 0;
    for (let i = 0; i < 800; i++) {
        if (i >= 400) {
            x = m;
            y = 200 + Math.sqrt(40000 - Math.pow(x - 200, 2));
            m--;
        } else {
            x = m;
            y = 200 - Math.sqrt(40000 - Math.pow(x - 200, 2));
            m++;
        }
        x_arr[i] = x - 60;
        y_arr[i] = parseInt(y) - 25;
    }

    /*
     *原型轨迹
     */
    let gitMove = document.getElementById('github_a');
    let weiboMove = document.getElementById('weibo_a')
    let blogMove = document.getElementById('blog_a')
    let g_num = 0;
    let w_num = 0;
    let b_num = 0;

    function movegit() {
        if (g_num <= 750) {
            // gitMove.css({"left":x_arr[g_num] + "px","top":y_arr[g_num]+"px"});
            gitMove.style.left = x_arr[g_num] + 'px';
            gitMove.style.top = y_arr[g_num] + 'px';
            g_num += 3;
        } else {
            clearInterval(gitTimer);
        }
    }

    function moveweibo() {
        if (w_num <= 400) {
            // weiboMove.css({"left":x_arr[w_num]+"px","top":y_arr[w_num]});
            weiboMove.style.left = x_arr[w_num] + 'px';
            weiboMove.style.top = y_arr[w_num] + 'px';
            w_num += 3;
        } else {
            clearInterval(weiTimer);
        }
    }

    function moveblog() {
        if (b_num <= 90) {
            // blogMove.css({"left":x_arr[b_num]+"px","top":y_arr[b_num]});
            blogMove.style.left = x_arr[b_num] + 'px';
            blogMove.style.top = y_arr[b_num] + 'px';
            b_num += 3;
        } else {
            clearInterval(blogTimer);
        }
    }

    movegit();
    let gitTimer = setInterval(movegit, 1);
    moveweibo();
    let weiTimer = setInterval(moveweibo, 1);
    moveblog();
    let blogTimer = setInterval(moveblog, 1);

    //第一屏文字加载
    let boxOneTimer = setInterval(boxOne, 800);
})();

let f_btn = document.getElementById("float_btn").children;
let nav_ul = document.getElementById('nav_ul').children;
let wrapBox = document.getElementById("wrapBox");
let foot = document.getElementById("foot");
let arrow = document.getElementsByClassName("arrow_img");
let bg = document.getElementsByClassName("box_bg");
let boxs = document.getElementsByClassName("box");
let indexs = 0;
let process = document.getElementById('bar_container').children;//进度条
let box02_flag = false;       //还未曾到第二屏
let box02_text = document.getElementById('box02_text').children; //第二屏文字
let box02_index = 0;
let box02_timer;


function hasClass(obj, cls) {
    return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addActive() {
    for (let i = 0; i < process.length; i++) {
        if (!hasClass(process[i], 'active')) {
            process[i].className += ' active';
        }
    }
    // process[100].className += ' active';
}

// 第二屏动画
function boxTow() {
    // box02_index = box02_text.length;
    if (box02_index >= 0 && box02_index < box02_text.length) {
        box02_text[box02_index].style.right = '0';
        box02_index++;
    } else {
        clearInterval(box02_timer);
    }
}


//滚动函数
function divMove(obj, overHeight, num) {
    if (obj.offsetTop >= overHeight) {
        //向下滚动
        let timer;
        timer = setInterval(function () {
            if ((obj.offsetTop - overHeight) <= num && (obj.offsetTop - overHeight) > 0) {
                obj.style.top = overHeight + 'px';
                clearInterval(timer);
            } else if ((obj.offsetTop - overHeight) > num) {
                obj.style.top = obj.offsetTop - num + 'px';
            }
            /*           else{
                           //停止滚动
                           //obj.style.top = obj.offsetTop - num + 'px';
                           clearInterval(timer);
                       }*/
        }, 10);
    } else if (obj.offsetTop < overHeight) {
        //向上滚动
        let timer;
        timer = setInterval(function () {
            if ((overHeight - obj.offsetTop) <= num && (overHeight - obj.offsetTop) > 0) {
                //最后定位
                obj.style.top = overHeight + 'px';
                clearInterval(timer);
            } else if ((overHeight - obj.offsetTop) > num) {
                //继续滚动
                obj.style.top = obj.offsetTop + num + 'px';
            }
            /*            else{
                            //停止滚动
                            clearInterval(timer);
                        }*/
        }, 10);
    }
}

function btnChange(index, flag, speed) {
    let height = window.innerHeight;
    let fh = foot.offsetHeight;
    for (let n = 0; n < f_btn.length; n++) {
        f_btn[n].style.background = "transparent";
    }

    for (let n = 0; n < nav_ul.length; n++) {
        nav_ul[n].style.color = '#000000';
    }

    if (flag) {
        if (index <= 0) {
            indexs = 0;
            f_btn[0].style.background = "#ffffff";
            //wrapBox.style.top = "0";
            nav_ul[0].style.color = 'red';
            divMove(wrapBox, 0, 20 * speed);
        } else if (index > 0 && index <= 3) {
            f_btn[index].style.background = "#ffffff";
            //wrapBox.style.top = (-height * indexs) +"px";
            nav_ul[index].style.color = 'red';
            divMove(wrapBox, -(height * indexs), 20 * speed);
        } else if (index == 4) {
            indexs = 4;
            //wrapBox.style.top = (-height * 3 - fh) +"px";
            divMove(wrapBox, (-height * 3 - fh), 20 * speed);
            nav_ul[index].style.color = 'red';
        } else {
            indexs = 4;
        }
    } else {
        if (index <= 0) {
            indexs = 0;
            f_btn[0].style.background = "#ffffff";
            nav_ul[0].style.color = 'red';
            wrapBox.style.top = "0";
        } else if (index > 0 && index <= 3) {
            f_btn[index].style.background = "#ffffff";
            nav_ul[index].style.color = 'red';
            wrapBox.style.top = (-height * indexs) + "px";
        } else if (index == 4) {
            indexs = 4;
            wrapBox.style.top = (-height * 3 - fh) + "px";
            nav_ul[index].style.color = 'red';
        } else {
            indexs = 4;
        }
    }
    if (indexs == 1 && !box02_flag) {
        box02_flag = true;
    }
    if (box02_flag) {
        //第二屏动画
        let timer = setTimeout(addActive, 1000);
        box02_timer = setInterval(boxTow, 0);
    }
}


//浮动点击事件
for (let i = 0; i < f_btn.length; i++) {
    f_btn[i].indexs = i;
    f_btn[i].onclick = function () {
        let speed = Math.abs(indexs - this.indexs);
        indexs = this.indexs;
        btnChange(indexs, true, speed);
    }
}

//浮动nav事件
for (let i = 0; i < nav_ul.length; i++) {
    nav_ul[i].indexs = i;
    nav_ul[i].onclick = function () {
        let speed = Math.abs(indexs - this.indexs);
        indexs = this.indexs;
        btnChange(indexs, true, speed);
    }
}


//箭头点击事件
for (let i = 0; i < arrow.length; i++) {
    arrow[i].indexs = i;
    arrow[i].onclick = function () {
        indexs = this.indexs + 1;
        btnChange(indexs, true, 1);
    }
}

let oB = true;

//鼠标滑动事件
let scrollFunc = function (e) {
    let direct = 0;
    e = e || window.event;
    if (e.wheelDelta) {   //IE/Opera/Chrome
        if (oB == true) {
            //向下滑动
            if (e.wheelDelta >= 120) {
                oB = false;
                indexs--;
                btnChange(indexs, true, 1);
                setTimeout(function () {
                    oB = true;
                }, 700);

            } else if (e.wheelDelta <= -120) {
                oB = false;
                //向上滑动
                indexs++;
                btnChange(indexs, true, 1);
                setTimeout(function () {
                    oB = true;
                }, 700);
            }
        }

    } else if (e.detail) {//Firefox
        if (oB) {
            if (e.detail >= 3) {
                oB = false;
                indexs++;
                btnChange(indexs, true, 1);
                setTimeout(function () {
                    oB = true;
                }, 700);
                console.log('ss');
            } else if (e.detail <= -3) {
                oB = false;
                indexs--;
                btnChange(indexs, true, 1);
                setTimeout(function () {
                    oB = true;
                }, 700);
            }
        }
    }
}


/*注册事件*/
if (document.addEventListener) {
    document.addEventListener('DOMMouseScroll', scrollFunc, false);//W3C
}
window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome/Safari


let iB = true;
//监听窗口改变
window.onresize = function () {
    document.getElementsByTagName("html")[0].style.fontSize = document.documentElement.clientWidth / 20 + 'px';
    let h = window.innerHeight;
    let w = window.innerWidth;
    if (w / h >= 1920 / 1080) {
        iB = true;
        imgChange(iB);
        btnChange(indexs);

    } else {
        iB = false;
        imgChange(iB);
        btnChange(indexs, false);
    }
}

function imgChange(iB) {
    let h = window.innerHeight;
    let w = window.innerWidth;
    if (iB) {
        for (let i = 0; i < bg.length; i++) {
            bg[i].style.width = w + 'px';
            bg[i].style.height = w * (1080 / 1920) + 'px';
            bg[i].style.top = -(w * (1080 / 1920) - h) / 2 + 'px';
            boxs[i].style.width = w + 'px';
            boxs[i].style.height = h + 'px';
            bg[i].style.left = '0';
        }
    } else {
        for (let i = 0; i < bg.length; i++) {
            bg[i].style.height = h + 'px';
            bg[i].style.width = h * (1920 / 1080) + 'px';
            bg[i].style.left = -(h * (1920 / 1080) - w) / 2 + 'px';
            boxs[i].style.width = w + 'px';
            boxs[i].style.height = h + 'px';
            bg[i].style.top = '0';
        }

    }
}

//手势事件
let startY, endY;
window.addEventListener('touchstart', touchStart, false);
window.addEventListener('touchmove', touchMove, false);
window.addEventListener('touchend', touchEnd, false);

function touchStart(event) {
    // let touch =   event.originalEvent.targetTouches[0];
    // startY = touch.pageY;
    startY = event.touches[0].clientY;
}

function touchMove(event) {
    // let touch =  event.originalEvent.changedTouches[0];
    // endY = touch.pageY;
    endY = event.touches[0].clientY;
}

function touchEnd(event) {
    if (endY - startY > 100) {
        //向上滑动
        indexs = this.indexs - 1;
        btnChange(indexs, true, 1);
    } else if (startY - endY > 100) {
        //向下滑动
        indexs = this.indexs + 1;
        btnChange(indexs, true, 1);
    }

}

function f1() {
    window.open('https://steam.vmu.me/server/binglin.top:27201');
}

function f2() {
    window.open('https://steam.vmu.me/server/binglin.top:28901');
}

function f3() {
    window.open('https://steam.vmu.me/server/binglin.top:28801');
}

function f4() {
    window.open('https://steam.vmu.me/server/binglin.top:27401');
}

function f5() {
    window.open('https://steam.vmu.me/server/binglin.top:27501');
}

function f6() {
    window.open('https://steam.vmu.me/server/binglin.top:27601');
}

function f7() {
    window.open('https://steam.vmu.me/server/binglin.top:27301');
}

function f8() {
    window.open('https://steam.vmu.me/server/binglin.top:25505');
}

function f9() {
    window.open('https://steam.vmu.me/server/binglin.top:27801');
}
